package com.base.kongjian.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.base.kongjian.dao.WealthSystemDao;
import com.base.kongjian.entity.WealthRec;
import com.base.kongjian.entity.WealthSystem;
import com.base.kongjian.service.WealthRecService;
import com.base.kongjian.service.WealthSystemService;

import cn.mysteel.util.ObjectUtils;

@Service("wealthSystemService")
public class WealthSystemServiceImpl implements WealthSystemService
{
	@Autowired
	private WealthSystemDao wealthSystemDao;
	@Autowired
	private WealthRecService wealthRecService;
	
	@Override
	public void saveWS(WealthSystem wealthSystem)
	{
		Assert.notNull (wealthSystem);
		
		long memberId = wealthSystem.getMemberId ();
		
		WealthSystem ws = getWSByMemberId (memberId);
		
		if(ObjectUtils.isNull (ws))
		{		
			wealthSystemDao.save (wealthSystem);
		}
	}
	
	@Override
	public void deleteWSByMemberId(long memberId)
	{
		WealthSystem ws = getWSByMemberId (memberId);
		
		if(ObjectUtils.notNull(ws))
		{
			wealthSystemDao.deleteByMemberId (memberId);
		}
	}
	
	@Override
	public WealthSystem getWSByMemberId(long memeberId)
	{
		
		return wealthSystemDao.getByMemberId (memeberId);
	}
	
	@Override
	public void updateWS(WealthSystem wealthSystem)
	{
		Assert.notNull (wealthSystem);
		
		WealthSystem ws = wealthSystemDao.getByMemberId (wealthSystem.getMemberId ());
		
		if(ObjectUtils.notNull (ws))
		{
			ws.setDiamondNum (wealthSystem.getDiamondNum ());
			ws.setFansNum (wealthSystem.getFansNum ());
			ws.setFlowerNum (wealthSystem.getFlowerNum ());
			ws.setLoverStarNum (wealthSystem.getLoverStarNum ());
			ws.setMemberId (wealthSystem.getMemberId ());
			
			wealthSystemDao.update (ws);
		}else
		{
			saveWS(wealthSystem);
		}
	}
	
	@Override
	public void inCome(long memberId, String wealthName)
	{
		WealthSystem ws = getWSByMemberId (memberId);
		
		if(ObjectUtils.isNull (ws))
		{
			 ws =  new WealthSystem();
			 
			 ws.setMemberId (memberId);
			 ws.setDiamondNum (0);
			 ws.setFansNum (0);
			 ws.setFlowerNum (0);
			 ws.setLoverStarNum (0);
			 
			 wealthSystemDao.save (ws);
		}else{	
			if(wealthName.equals ("flower"))
			{
				ws.setFlowerNum (ws.getFlowerNum ()+1);
			}else if(wealthName.equals ("loverStar"))
			{
				ws.setLoverStarNum (ws.getLoverStarNum ()+1);
			}else if(wealthName.equals ("diamond"))
			{
				ws.setDiamondNum (ws.getDiamondNum ()+1);
			}else if(wealthName.equals ("fans")){
				ws.setFansNum (ws.getFansNum ()+1);
			}
		}
		
		wealthSystemDao.update (ws);
	}
	
	@Override
	public void outLay(long memberId, String wealthName)
	{
		WealthSystem ws = getWSByMemberId (memberId);
		
		if(ObjectUtils.notNull (ws))
		{
			if(wealthName.equals ("flower")&&ws.getFlowerNum ()>0)
			{
				ws.setFlowerNum (ws.getFlowerNum ()-1);
			}else if(wealthName.equals ("loverStar")&&ws.getLoverStarNum ()>0)
			{
				ws.setLoverStarNum (ws.getLoverStarNum ()-1);
			}else if(wealthName.equals ("diamond")&&ws.getDiamondNum ()>0)
			{
				ws.setDiamondNum (ws.getDiamondNum ()-1);
			}else if(wealthName.equals ("fans")&&ws.getFansNum ()>0){
				ws.setFansNum (ws.getFansNum ()-1);
			}
			
			wealthSystemDao.update (ws);
		}	
	}
	
	@Override
	public void WealthtransformSystem(long memberId, long workId, int type,int workKind)
	{
;			WealthSystem ws = getWSByMemberId (memberId);

			if(ObjectUtils.isNull (ws))
			{
				ws = new WealthSystem ();
				ws.setMemberId (memberId);
				
				wealthSystemDao.save (ws);
			}
			Map<String,Integer> map = new HashMap<String,Integer>();
		
			map = wealthTransformRule (type ,workId ,workKind);
			
			for(String key : map.keySet ())
			{
				if(ObjectUtils.notNull (key)&&map.get (key)!=0)
				{
					inCome (memberId ,key);
				}
			}		
	}
	
	@Override
	public void WealthExchangeSystem(long memberId, String originalWealName,String goalWealthName, long ExchangeNum)
	{
		WealthSystem ws = getWSByMemberId (memberId);
		Map<String,Object> map = new HashMap<String,Object>();
		
		if(ObjectUtils.notNull (ws))
		{
			map = WealthExchangeRule (originalWealName ,goalWealthName ,ExchangeNum);
			
			for(String key : map.keySet ())
			{
				if(ObjectUtils.notNull (key)&&ObjectUtils.notNull(map.get (key))&&key.equals ("income"))
				{
					for(int i = 0;i<(Long)map.get (key);i++)
					{
						inCome (memberId ,goalWealthName);
					}
				}
				if(ObjectUtils.notNull (key)&&ObjectUtils.notNull(map.get (key))&&key.equals ("outlay"))
				{
					for(int i = 0;i<(Long)map.get (key);i++)
					{
						outLay (memberId ,originalWealName);
					}
				}
			}
		}
	}

	@Override
	public Map<String, Integer> wealthTransformRule(int type, long workId,int workKind)
	{
		Map<String,Integer> returnMap = new HashMap<String,Integer>();
		Map<String,Object> params = new HashMap<String,Object>();
		
		params.put ("type" ,type);
		params.put ("workId" ,workId);
		params.put ("workKind" ,workKind);
		
		List<WealthRec> list =wealthRecService.getWRByMap (params);
		WealthRec wr  = null;
		if(ObjectUtils.notEmpty (list))
		{
			wr = list.get (0);
		}
		
		if(ObjectUtils.notNull (wr))
		{
			long hisR = wr.getHisRec ();
			//1-转发 ,2-收藏,3-点赞,4评论
			//（5次评论-一朵鲜花） （5个点赞-一朵鲜花），（5次转发-一颗爱心），（10次收藏-一颗钻石）
			if(type ==1)//转发
			{
				if(hisR+1>=5)
				{
					returnMap.put("loverStar",1);			
					wr.setNewRec (wr.getNewRec ()+hisR+1);
					wr.setHisRec (0);
				}else{
					returnMap.put("loverStar",0);		
					wr.setHisRec (hisR+1);
				}
			}else if(type==2)//收藏	
			{
				if(hisR+1>=10)
				{
					returnMap.put("diamond",1);			
					wr.setNewRec (wr.getNewRec ()+hisR+1);
					wr.setHisRec (0);
				}else{
					returnMap.put("diamond",0);		
					wr.setHisRec (hisR+1);
				}
			}else if(type==3)//点赞
			{
				if(hisR+1>=5)
				{
					returnMap.put("flower",1);			
					wr.setNewRec (wr.getNewRec ()+hisR+1);
					wr.setHisRec (0);
				}else{
					returnMap.put("flower",0);		
					wr.setHisRec (hisR+1);
				}
			}else if(type==4)//评论
			{
				if(hisR+1>=5)
				{
					returnMap.put("flower",1);			
					wr.setNewRec (wr.getNewRec ()+hisR+1);
					wr.setHisRec (0);
				}else{
					returnMap.put("flower",0);		
					wr.setHisRec (hisR+1);
				}
			}
			
			wealthRecService.updateWR (wr);
		}else
		{
			wr = new WealthRec ();
			wr.setHisRec (1);
			wr.setNewRec (0);
			wr.setType (type);
			wr.setWorkKind (workKind);
			wr.setWorkId (workId);
			
			wealthRecService.saveWR (wr);
			
			returnMap.put (null ,null);
		}
		
		return returnMap;
	}

	@Override
	public Map<String,Object> WealthExchangeRule(String originalWealName,String goalWealthName, long ExchangeNum)
	{
		Map<String,Object> returnMap = new HashMap<String,Object>();
		
		if(originalWealName.equals ("flower")&&goalWealthName.equals ("loverStar")&&ExchangeNum%10==0)
		{
			returnMap.put ("income" ,ExchangeNum/10);
			returnMap.put ("outlay" ,ExchangeNum);
			
		}else if(originalWealName.equals ("flower")&&goalWealthName.equals ("diamond")&&ExchangeNum%100==0)
		{
			returnMap.put ("income" ,ExchangeNum/100);
			returnMap.put ("outlay" ,ExchangeNum);
			
		}else if(originalWealName.equals ("loverStar")&&goalWealthName.equals ("diamond")&&ExchangeNum%10==0)
		{
			returnMap.put ("income" ,ExchangeNum/10);
			returnMap.put ("outlay" ,ExchangeNum);
			
		}else if(originalWealName.equals ("diamond")&&goalWealthName.equals ("loverStar"))
		{
			returnMap.put ("income" ,ExchangeNum*10);
			returnMap.put ("outlay" ,ExchangeNum);
			
		}else if(originalWealName.equals ("diamond")&&goalWealthName.equals ("flower"))
		{
			returnMap.put ("income" ,ExchangeNum*100);
			returnMap.put ("outlay" ,ExchangeNum);
			
		}else{
			returnMap.put (null ,null);
		}
		
		return returnMap;
	}
}
